var ws = require("nodejs-websocket");
var db = require('./db');
var cmd = require('./command');
var config = require('./config');


console.log("serverCache start a " + config.serverPort);

var server = ws.createServer(function (conn) {
	console.log("New connection");

	conn.on("text", function (str) {
		cmd.exec(conn, str);
	});

	conn.on("close", function (code, reason) {
		console.log("Connection closed ");
		//db.close();
	});
}).listen(config.serverPort);

server.on('close', function() {
	db.close();
	console.log("server down");
});


//db.query('select * from users', null, function(error, results, fields) {
//	//if (err) throw err;
//	console.log(results);
//});

